package com.atguigu.vod.controller;

import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.vod.model.v20170321.DeleteVideoRequest;
import com.aliyuncs.vod.model.v20170321.DeleteVideoResponse;
import com.aliyuncs.vod.model.v20170321.GetVideoPlayAuthRequest;
import com.aliyuncs.vod.model.v20170321.GetVideoPlayAuthResponse;
import com.atguigu.base.ExceptionHnadler.CustomException;
import com.atguigu.utils.R;
import com.atguigu.vod.service.VodService;
import com.atguigu.vod.utils.ConstantVodUtils;
import com.atguigu.vod.utils.InitVodClient;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.util.List;

@RestController
@RequestMapping("/eduvod/video")
//@CrossOrigin
public class VodController {
    @Resource
    private VodService vodService;
    //将视频上传到aly的视频点播处
    @PostMapping("/uploadVideo")
    public R uploadVideo(MultipartFile file){
        String videoId=vodService.uploadVideo(file);
        return R.ok().data("videoId",videoId);
    }
    //根据小节id 将aly处的视频删除
    @DeleteMapping("/deleteVideo/{id}")
    public R deleteVideo(@PathVariable("id") String id){
        vodService.deleteVideo(id);
        return R.ok();
    }
    //根据课程id 将aly处的多个视频删除
    @DeleteMapping("/deleteBatchVideo")
    public R deleteBatchVideo(@RequestParam("videoIds") List<String> videoIds){
        vodService.deleteBatchVideo(videoIds);
        return R.ok();
    }
    //根据视频id视频凭证
    @GetMapping("/getPlayAuth/{videoId}")
    public R getPlayAuth(@PathVariable("videoId")String videoId){
        try{
           //创建初始化对象
           DefaultAcsClient client = InitVodClient.initVodClient(ConstantVodUtils.KEY_ID, ConstantVodUtils.KEY_SECRET);
           GetVideoPlayAuthResponse response=new GetVideoPlayAuthResponse();
           GetVideoPlayAuthRequest request=new GetVideoPlayAuthRequest();
           request.setVideoId(videoId);
           response=client.getAcsResponse(request);
           String playAuth = response.getPlayAuth();
           return R.ok().data("playAuth",playAuth);
        }catch (Exception e){
            throw new CustomException(20001,"获取视频凭证失败");
        }
    }
}
